package net.entelijan.cobean.examples.showcase.config.filesel;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Arrays;

import javax.swing.JFileChooser;

import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.finder.JFileChooserFinder;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JComboBoxFixture;
import org.fest.swing.fixture.JFileChooserFixture;
import org.junit.Test;

public class ConfigFileselFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(ConfigFileselFestTestCase.class);

	public void testPattern() {
		CobeanTestUtil.createTestFilesAndDirectories();
		ConfigFileselPanel panel = new ConfigFileselPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigFileselShowcase sc = new ConfigFileselShowcase();

//			JComboBoxFixture directoryFileSelFieldComboBox = frameFix
//					.comboBox("directoryFileSelFieldComboBox");
//			JButtonFixture directoryFileSelChooserButton = frameFix
//					.button("directoryFileSelChooserButton");
//
//			JComboBoxFixture fileFileSelFieldComboBox = frameFix
//					.comboBox("fileFileSelFieldComboBox");
//			JButtonFixture fileFileSelChooserButton = frameFix
//					.button("fileFileSelChooserButton");
			sc.init(panel);
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromFileChooser00() {
		CobeanTestUtil.createTestFilesAndDirectories();
		ConfigFileselPanel panel = new ConfigFileselPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigFileselShowcase sc = new ConfigFileselShowcase();

			JButtonFixture directoryFileSelChooserButton = frameFix
					.button("directoryFileSelChooserButton");
			sc.init(panel);

			directoryFileSelChooserButton.click();
			JFileChooserFixture chooser = findFileChooser(frameFix);
			File homeDir = new File(System.getProperty("user.home"));
			chooser.setCurrentDirectory(homeDir);
			pause(100);
			File testdir = new File(homeDir, ".testdir");
			chooser.setCurrentDirectory(testdir);
			pause(100);
			chooser.approveButton().click();
			pause(100);
			JComboBoxFixture directoryFileSelFieldComboBox = frameFix
					.comboBox("directoryFileSelFieldComboBox");

			//Check that the selected directory is displayed
			directoryFileSelFieldComboBox.requireSelection(testdir
					.getAbsolutePath());
			// Check if the model has also changed
			assertEquals(testdir, sc.getModel().getDirectory());

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFromFileChooser01() {
		CobeanTestUtil.createTestFilesAndDirectories();
		ConfigFileselPanel panel = new ConfigFileselPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigFileselShowcase sc = new ConfigFileselShowcase();

			JButtonFixture directoryFileSelChooserButton = frameFix
					.button("directoryFileSelChooserButton");

			sc.init(panel);
			pause(100);

			directoryFileSelChooserButton.click();
			JFileChooserFixture chooser = findFileChooser(frameFix);
			assertEquals(chooser.component().getFileSelectionMode(),
					JFileChooser.DIRECTORIES_ONLY);
			File homeDir = new File(System.getProperty("user.home"));
			chooser.setCurrentDirectory(homeDir);
			File testdir = new File(homeDir, ".testdir");
			chooser.setCurrentDirectory(testdir);
			File dir00 = new File(testdir, "dir00");
			chooser.setCurrentDirectory(dir00);
			pause(100);
			chooser.approveButton().click();
			pause(100);
			JComboBoxFixture directoryFileSelFieldComboBox = frameFix
					.comboBox("directoryFileSelFieldComboBox");

			// Test if the correct directory is displayed
			directoryFileSelFieldComboBox.requireSelection(dir00
					.getAbsolutePath());
			// Check if the model has also changed
			assertEquals(dir00, sc.getModel().getDirectory());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectDirFromCombo() {
		CobeanTestUtil.createTestFilesAndDirectories();
		ConfigFileselPanel panel = new ConfigFileselPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigFileselShowcase sc = new ConfigFileselShowcase();

			JButtonFixture directoryFileSelChooserButton = frameFix
					.button("directoryFileSelChooserButton");

			sc.init(panel);
			pause(100);

			File dir00;

			// Select dir00 from chooser
			{
				directoryFileSelChooserButton.click();
				JFileChooserFixture chooser = findFileChooser(frameFix);
				assertEquals(chooser.component().getFileSelectionMode(),
						JFileChooser.DIRECTORIES_ONLY);
				File homeDir = new File(System.getProperty("user.home"));
				chooser.setCurrentDirectory(homeDir);
				File testdir = new File(homeDir, ".testdir");
				chooser.setCurrentDirectory(testdir);
				File dirdir = new File(testdir, "dir00");
				dir00 = dirdir;
				chooser.setCurrentDirectory(dirdir);
				pause(100);
				chooser.approveButton().click();
				pause(100);
			}
			// Select dir01 from chooser
			{
				directoryFileSelChooserButton.click();
				JFileChooserFixture chooser = findFileChooser(frameFix);
				assertEquals(chooser.component().getFileSelectionMode(),
						JFileChooser.DIRECTORIES_ONLY);
				File homeDir = new File(System.getProperty("user.home"));
				chooser.setCurrentDirectory(homeDir);
				File testdir = new File(homeDir, ".testdir");
				chooser.setCurrentDirectory(testdir);
				File dirdir = new File(testdir, "dir01");
				chooser.setCurrentDirectory(dirdir);
				pause(100);
				chooser.approveButton().click();
				pause(100);
			}

			JComboBoxFixture directoryFileSelFieldComboBox = frameFix
					.comboBox("directoryFileSelFieldComboBox");

			int idx = -1;
			int cnt = 0;
			for (String content : directoryFileSelFieldComboBox.contents()) {
				if (content.matches(".*dir00.*")) {
					idx = cnt;
					break;
				}
				cnt++;
			}
			if (idx < 0) {
				fail("Could not find 'dir00' in '"
						+ Arrays.toString(directoryFileSelFieldComboBox.contents()) + "'");
			}

			directoryFileSelFieldComboBox.selectItem(idx);
			// Test if the correct directory is displayed
			directoryFileSelFieldComboBox.requireSelection(dir00
					.getAbsolutePath());
			// Check if the model has also changed
			assertEquals(dir00, sc.getModel().getDirectory());

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSelectFileFromFileChooser() {
		CobeanTestUtil.createTestFilesAndDirectories();
		ConfigFileselPanel panel = new ConfigFileselPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigFileselShowcase sc = new ConfigFileselShowcase();

			JButtonFixture fileFileSelChooserButton = frameFix
					.button("fileFileSelChooserButton");

			sc.init(panel);

			fileFileSelChooserButton.click();
			JFileChooserFixture chooser = findFileChooser(frameFix);
			File homeDir = new File(System.getProperty("user.home"));
			chooser.setCurrentDirectory(homeDir);
			pause(100);
			File testdir = new File(homeDir, ".testdir");
			chooser.setCurrentDirectory(testdir);
			File file00 = new File(testdir, "test01.txt");
			chooser.selectFile(file00);
			pause(100);
			chooser.approveButton().click();
			pause(100);
			JComboBoxFixture directoryFileSelFieldComboBox = frameFix
					.comboBox("fileFileSelFieldComboBox");

			//Check that the selected directory is displayed
			directoryFileSelFieldComboBox.requireSelection(file00
					.getAbsolutePath());
			// Check if the model has also changed
			assertEquals(file00, sc.getModel().getFile());

		} finally {
			frameFix.cleanUp();
		}

	}

	/**
	 * Finds a filechooser if it was opened before
	 * @param frameFix The frame from whitch the file chooser was opened.
	 * @return The found file chooser
	 */
	private JFileChooserFixture findFileChooser(FrameFixture frameFix) {
		JFileChooserFinder findFileChooser = JFileChooserFinder
				.findFileChooser();
		JFileChooserFixture chooser = findFileChooser.using(frameFix.robot);
		return chooser;
	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
